<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author sqc
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'quotation:jbSupplier:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsQuotationJbSupplierForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { JbSupplier, jbSupplierSave, jbSupplierForm } from '/@/api/quotation/jbSupplier';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('quotation.jbSupplier');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<JbSupplier>({} as JbSupplier);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增供应商基本信息表') : t('编辑供应商基本信息表'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('供应商名称'),
      field: 'supplierName',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      required: true,
    },
    {
      label: t('联系人姓名'),
      field: 'contactsName',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('联系人电话'),
      field: 'contactsMobile',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('地址'),
      field: 'supplierAddress',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('法人'),
      field: 'legalPerson',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('LOGO路径'),
      field: 'supplierLogo',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('统一信用代码'),
      field: 'creditCode',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('报价单显示的简称'),
      field: 'shortName',
      component: 'Input',
      componentProps: {
        maxlength: 30,
      },
    },
    {
      label: t('简称的拼音首字母，用于快速搜索'),
      field: 'pinyinCode',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
    },
    {
      label: t('图片上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'jbSupplier_image',
        uploadType: 'image',
      },
      colProps: { md: 24, lg: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await jbSupplierForm(data);
    record.value = (res.jbSupplier || {}) as JbSupplier;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        supplierId: record.value.supplierId,
      };
      // console.log('submit', params, data, record);
      const res = await jbSupplierSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
